package br.mestrado.unb.bs.sensor;

import br.mestrado.unb.bs.communication.IBluetoothWriter;
import br.mestrado.unb.bs.model.configuration.SensorConfiguration;
import br.mestrado.unb.bs.model.configuration.SensorIdentification;
import br.mestrado.unb.bs.model.constant.DataPackageConstant;
import br.mestrado.unb.bs.model.sensor.DataPackageWrapper;
import br.mestrado.unb.bs.model.sensor.Payload;
import br.mestrado.unb.bs.model.sensor.Spo2Payload;
import br.mestrado.unb.bs.util.BluetoothSenderConstant;

public class SPO2SensorThread extends ThreadSensor{


	public SPO2SensorThread(SensorConfiguration config, SensorIdentification id, IBluetoothWriter writer) {
		super(config, id, writer);
		setName(this.getClass().getName());
	}

	@Override
	protected void sendValue() throws Exception {
		
		Payload objPayload = this.config.getConfiguration(id);
		if(objPayload instanceof Spo2Payload){
			Spo2Payload o2Payload = (Spo2Payload) objPayload;

			Spo2Payload payload = new Spo2Payload();
			payload.setBattery(o2Payload.getBattery());
			payload.setSampleFrequency(o2Payload.getSampleFrequency());
			payload.setCategory(DataPackageConstant.temp_category);
			payload.setHighOxygenation(o2Payload.getHighOxygenation());
			payload.setLowOxygenation(o2Payload.getLowOxygenation());
			payload.setHighPulseRate(o2Payload.getHighPulseRate());
			payload.setLowPulseRate(o2Payload.getLowPulseRate());
		
			this.period = BluetoothSenderConstant.TEN_SECONDS/(int)payload.getSampleFrequency();
			DataPackageWrapper dataPackage = new DataPackageWrapper(payload);
			bluetooth.write(dataPackage.toByteStream());
			
			window.addMessage("(SPO2 , " + this.id.getSensorId() + ")" + ": Enviou." + payload.toString());
		}
	}

	

}
